﻿using AllInServiceAuthorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Cmp.Web.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        private readonly IJWTService _jWTService;

        /// <summary>
        /// 实例化。
        /// </summary>
        /// <param name="jWTService">注入服务。</param>
        public AuthenticationController(IJWTService jWTService)
        {
            _jWTService = jWTService;
        }

        /// <summary>
        /// 根据用户名和密码获取 Token。
        /// </summary>
        /// <param name="userName">用户名。</param>
        /// <param name="password">密码。</param>
        /// <returns></returns>
        [HttpPost(Name = "GetToken")]
        public string Login(string userName, string password)
        {
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                //此处可使用配置或数据库信息实现多租户
                if (string.Compare(userName, "123", true) == 0 && string.Compare(password, "123", true) == 0)
                {
                    string token = _jWTService.GetToken(userName, password);
                    return JsonConvert.SerializeObject(new
                    {
                        Result = true,
                        Token = token
                    });
                }
            }
            return string.Empty;
        }

    }
}
